package team.yidong.fenda.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.HospitalField;
import team.yidong.fenda.service.HospitalFieldService;
import team.yidong.fenda.service.HospitalService;
import team.yidong.fenda.utils.ImageCheck;
import team.yidong.fenda.utils.QiNiuYunUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by xuanlong on 2017/10/16.
 */
@Controller
public class HospitalFieldController {
    @Autowired
    HospitalFieldService hospitalFieldService;
    @Autowired
    HospitalService hospitalService;
    @Autowired
    ImageCheck imageCheck;
    @Autowired
    QiNiuYunUtil qiNiuYunUtil;

    @RequiresPermissions("hospitalField")
    @RequestMapping("/hospitalField")
    public String hospitalField(HttpSession session, Model model) {

        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = (String) session.getAttribute("loginSysUserName");
        model.addAttribute("sysName", sysName);

        if (hospitalId != 1) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(new String[]{"hospital", "hospitalPermissionSet", "userSet", "masterList"});
            model.addAttribute("hospitals", JSONArray.fromObject(hospitalService.findHospitalById(hospitalId), jsonConfig));
        } else {
            JSONArray jsonArray = JSONArray.fromObject(hospitalService.findAll());
            jsonArray.remove(0);
            model.addAttribute("hospitals", jsonArray);
        }

        return "hospitalField";

    }


    @RequestMapping("/findHospitalField")
    @ResponseBody
    public JSONObject findHospitalField(HttpSession session, Integer limit, Integer offset, HttpServletRequest request, String hospitalName) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        return hospitalFieldService.findHospitalField(hospitalId, hospitalName, offset, limit);
    }

    @RequestMapping("/addHospitalField")
    @ResponseBody
    public String findHospitalField(@RequestParam String hospitalName, @RequestParam String hospitalFieldInfo) {

        hospitalFieldService.addHospitalField(hospitalName, hospitalFieldInfo);
        return "添加成功";
    }

    @RequestMapping("/deleteHospitalField")
    @ResponseBody
    public String deleteHospitalField(@RequestParam String jsonData) {
        JSONArray jsonArray = JSONArray.fromObject(jsonData);
        Iterable<HospitalField> hospitalFieldIterable = JSONArray.toCollection(jsonArray, HospitalField.class);
        hospitalFieldService.deleteHospitalField(hospitalFieldIterable);
        return "删除成功";
    }

    @RequestMapping("/updateHospitalField")
    @ResponseBody
    public String updateHospitalField(@RequestParam String jsonData, @RequestParam String oldFieldName) {

        hospitalFieldService.saveHospitalFieldAndQuestionClass(jsonData, oldFieldName);
        return "修改成功";
    }

    @RequestMapping("/updateFieldIcon")
    @ResponseBody
    public String updateFieldIcon(@RequestParam Integer fieldId, @RequestParam("fieldIcon") MultipartFile multipartFile) {
        String fileName = new String();
        if (!multipartFile.isEmpty()) {
            try {
                if (imageCheck.isImage(multipartFile.getBytes())) {
                    // 获取文件的后缀名
                    fileName = multipartFile.getOriginalFilename();
                    String suffixName = fileName.substring(fileName.lastIndexOf("."));
                    //uuid为文件命名
                    fileName = UUID.randomUUID() + suffixName;
                    qiNiuYunUtil.uploadFiles(multipartFile.getBytes(), fileName);
                } else {
                    return "不是图片文件";
                }
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败";
            }
        } else {
            return "文件为空";
        }
        return hospitalFieldService.updateFieldIcon(fieldId, fileName);
    }
}
